<?php
defined( '_VALID_MOS' ) or die('<script>window.location.href="http://www.kinifurniture.com";</script>');

$adm_functions->is_admin_login();
$section='Edit Product';
$adm_menu='
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td class="menudottedline" width="40%">
			<div class="pathway"><img src="assets/admin/'.$SITE_LOGO_ADMIN.'"/></div>	</td>
		<td class="menudottedline" align="right">
			<table cellpadding="0" cellspacing="0" border="0" id="toolbar">
				<tr valign="middle" align="center">
					<td>&nbsp;</td>
					<td>
					<a class="toolbar" href="javascript:kliksubmit();">
						<img src="show_files/save.png"  alt="Save" name="save" title="Save" align="middle" border="0" />				<br />Save</a>
					</td>
					<td>&nbsp;</td>
					<td>
						<a class="toolbar" href="admin.php?show=product_manager">
						<img src="show_files/cancel.png"  alt="Cancel" name="cancel" title="Cancel" align="middle" border="0" />				<br />Cancel</a>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<br />
';

if ($id)
{
	$qryedit="select * from produk where product_id=$id";
	$selectedit=$dbh->query($qryedit);

	while($sqlRowedit=mysql_fetch_object($selectedit))
	{
	$qrycoll="select * from koleksi";
	$Res=$dbh->query($qrycoll);
	$val_collection='';

	while($sqlRowcoll=mysql_fetch_object($Res))
	{
		$val_id_collection=$sqlRowcoll->id;
		$coll_name=$sqlRowcoll->nama;

		if ($inp_collection=='') $val_selected_coll=$coll;
		if ($val_id_collection==$val_selected_coll) $selected='selected'; else $selected='';
		$val_collection.='<option value="'.$val_id_collection.'" '.$selected.'>'.$coll_name.'</option>';
	}

		$val_id_product=$sqlRowedit->product_id;
		$val_title=$sqlRowedit->nama_produk;
		$val_size=$sqlRowedit->size;
		$val_nr=$sqlRowedit->nr;
		$val_description=$sqlRowedit->keterangan;
		$val_img_collection=$sqlRowedit->imglarge;
		if ($val_img_collection=='') $val_img_collection='noimg.png';
		if ($val_img_collection=='' || $val_img_collection=='noimg.png')
			$image='<input type="file" name="inp_upl_img"/>';
		else
			$image='<input class="inputbox" type="checkbox" name="inp_del_img" value="Y" /> Delete this image <font color="green">('.$val_img_collection.')</font>';
		$val_sample=$sqlRowedit->sample;
		$val_fsclabel=$sqlRowedit->fsclabel;

		if ($sqlRowedit->sample=='Y') 
		{
			$val_select1='selected';
			$val_select2='';
		}
			else 
		{
			$val_select1='';
			$val_select2='selected';
		}
		if ($sqlRowedit->fsclabel=='Y') 
		{
			$val_select3='selected';
			$val_select4='';
		}
			else 
		{
			$val_select3='';
			$val_select4='selected';
		}
	}
}

if($inp_title)
{
//echo "<script>alert('$inp_upl_img');</script>";
	if ($inp_del_img=='Y')
	{
		$dbh->query("update produk set 
			nama_produk='$inp_title', 
			koleksi='$inp_collection',
			size='$inp_size', 
			nr='$inp_nr', 
			sample='$inp_sample',
			fsclabel='$inp_fsclabel',
			imglarge='',
			keterangan='$inp_description'
			where product_id=$inp_id");
		@unlink("assets/product/".$inp_image);
		echo '<script>window.location.href="admin.php?show=product_manager&coll='.$coll.'";</script>';

	} else
	{
//$target = "upload/"; 
//$target = $target . basename( $_FILES['inp_img_uploaded']['name']) ; 
//$ok=1; 
//if(move_uploaded_file($_FILES['inp_img_uploaded']['tmp_name'], $target)) 
//{
//echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
//} 
//else {
//echo "Sorry, there was a problem uploading your file.";
//}


	if($_FILES['inp_upl_img']['name'])
	{
		$imageexists='N';
		$img_name=basename($_FILES['inp_upl_img']['name']);
		$qryname="select * from produk where imglarge='$img_name'";
		$selectname=$dbh->query($qryname);
		while($sqlRowedit=mysql_fetch_object($selectname))
		{
			$imageexists='Y';
		}
		if ($imageexists=='Y') 
		{
			echo '<script>alert("The Filename is already exists, try another filename");</script>';
		}
		else
		{
			$uploaddir = 'assets/product/';
			$uploadfile = $uploaddir . basename($_FILES['inp_upl_img']['name']);
			move_uploaded_file($_FILES['inp_upl_img']['tmp_name'], $uploadfile);
			$img_name=basename($_FILES['inp_upl_img']['name']);
			$qry_image='imglarge=\''.$img_name.'\',';
		}
	} else 	$qry_image='';

	if ($imageexists!='Y')
		{
	$dbh->query("update produk set 
		nama_produk='$inp_title',
		koleksi='$inp_collection',
		size='$inp_size', 
		nr='$inp_nr', 
		sample='$inp_sample',
		fsclabel='$inp_fsclabel',
		$qry_image
		keterangan='$inp_description'
		where product_id=$inp_id");
	echo '<script>window.location.href="admin.php?show=product_manager&coll='.$coll.'";</script>';
	}
	}
}
?>